# == Schema Information
#
# Table name: sales_contacts
#
#  id                   :integer          not null, primary key
#  firm_id              :integer
#  name                 :string(255)
#  email                :string(255)
#  phone                :string(255)
#  phone_ext            :string(255)
#  phone_same_with_firm :boolean
#  mobile               :string(255)
#  notes                :string(255)
#  active               :boolean          default(TRUE)
#  position             :string(255)
#  salutation           :string(255)
#  key_person           :boolean
#  created_at           :datetime         not null
#  updated_at           :datetime         not null
#

class Sales::Contact < ApplicationRecord
  extend Enumerize
  belongs_to :firm, :class_name => 'Sales::Firm', foreign_key: 'firm_id', optional: true

  has_many :follow_ups, :class_name => 'Sales::FollowUp', foreign_key: 'contact_id'
  
  validates_presence_of :name, message: '请填写联系人姓名'


  def to_s
    name
  end

  def full_phone
    ([phone,phone_ext].compact-['']).join('-')
  end

  def full_name
    _ = name
    _ << " - #{salutation}" unless salutation.blank?
    _ << "（#{position}）" unless position.blank?
    _
  end
  
  def key_person_flag
    '<i class="fa fa-star push-5-l" data-original-title="关键联系人" data-toggle="tooltip" style="color: gold"></i>'.html_safe if key_person
  end
end
